//
//  AnimationViewController.m
//  DemoV2
//
//  Created by 董一龙 on 2022/6/25.
//

#import "AnimationViewController.h"
#import "../Common.h"

@interface AnimationViewController ()
@property (nonatomic, strong, readwrite) UIImageView *animationView;
@property (nonatomic, strong, readwrite) UIButton *button;
@end

@implementation AnimationViewController

- (instancetype)init{
    if(self = [super init]){
        self.tabBarItem.title = @"动画";
        self.tabBarItem.image = kGetImage(@"kafei.png");
        self.view.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    CGFloat animationViewWidth = 80;
    self.animationView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - animationViewWidth) / 2, kSafeAreaTopHeight + kContentHeight - 200, animationViewWidth, animationViewWidth)];
    self.animationView.backgroundColor = [UIColor redColor];
    [self.animationView setImage:kGetImage(@"kafei.png")];
    [self.view addSubview:self.animationView];
    
    CGFloat buttonWidth = 80;
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake((SCREEN_WIDTH - buttonWidth)/2, SCREENH_HEIGHT - 180, buttonWidth, 40);
    MMViewBorderRadius(self.button, 8, 2, [UIColor blackColor]);
    self.button.titleLabel.font = [UIFont systemFontOfSize:12];
    self.button.backgroundColor = [UIColor lightGrayColor];
    [self.button setTitle:@"启动" forState:UIControlStateNormal];
    [self.view addSubview:self.button];
    [self.button addTarget:self action:@selector(startAnimation) forControlEvents:UIControlEventTouchUpInside];
}

- (void)startAnimation{
    CABasicAnimation *animation0 = [CABasicAnimation animationWithKeyPath:@"position"];
    animation0.fromValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH / 2, kSafeAreaTopHeight + kContentHeight - 200 + self.animationView.frame.size.height/2)];
    animation0.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH / 2, kSafeAreaTopHeight + 10)];
    animation0.duration = 10.0f;
    
    
    animation0.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    CAKeyframeAnimation *animation1 = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    animation1.keyTimes = @[@(0),@(0.5),@(0.8), @(1)];
    animation1.values = @[@(1), @(0.5),@(2),@(1)];
    animation1.duration = 10.0f;
    animation1.removedOnCompletion = NO;
    animation1.fillMode = kCAFillModeForwards;
    
    CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation2.byValue = @(M_PI * 2 * 15);
    animation1.duration = 10.0f;
    
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    animationGroup.duration = 10.0f;
    animationGroup.fillMode = kCAFillModeForwards;
    animationGroup.removedOnCompletion = NO;
    
    
    animationGroup.animations = @[animation0, animation1, animation2];
    [self.animationView.layer addAnimation:animationGroup forKey:@"LearnAnimation"];
}
@end
